# -*- coding: utf-8 -*-
# from scrapy.utils.project import get_project_settings
import pymysql
import os
import base64
from scrapy import cmdline
# setting=get_project_settings()

# HOST='127.0.0.1'
# PORT=3306
# USER='root'
# PASSWD=''
# DB='ceshi'
# CHARSET='utf8mb4'
#
# conn = pymysql.connect(host=HOST, port=PORT, user=USER, passwd=PASSWD, db=DB,charset=CHARSET)
# cur=conn.cursor()
# sql="select Elong_Username,Elong_Password from hotel_users"
# cur.execute(sql)
# users = cur.fetchall()
#
# for user in users:
#     if user[0]!=None and user[1]!=None:
#         username = user[0]
#         password = user[1]
#         # cmdline.execute(('scrapy crawl elong -a username=%s -a password=%s'%(username,pa)).split())
#         begin = 'scrapy crawl elong -a username=%s -a password=%s'%(username,password)
#         os.system(begin)


import redis
RHOST = '10.200.20.200'
RPORT = 6379
RPASSWORD = 'zhangym930920'
RDB = 1
r = redis.Redis(host=RHOST, port=RPORT, db=RDB,charset='utf-8',password=RPASSWORD,decode_responses=True)


while True:
    datas = r.brpop('hotel_users_elong')[1]
    if datas:
        z = eval(datas)
        CRS_ID = z[2]
        username = z[0]
        password = base64.b64encode(str(z[1]).encode())
        batch_no = z[3]
        print(CRS_ID,username,password,batch_no)
        begin = 'scrapy crawl elong -a CRS_ID=%s -a username=%s -a password=%s -a batch_no=%s' % (CRS_ID,username, password,batch_no)
        os.system(begin)
    else:
        break